<?php
//include_once "test2.php";
$popsize=50;	//banyaknya kemungkinan
$gen=10;		//banyaknya generasi
$matkul=35;		//banyaknya mata kuliah
$jadwal=30;		//banyaknya slot jadwal
$pm=0.05;		//persen mutasi 10%
$gens=1;
$pop_best=round($popsize/2);				//50%
//$pop_rand=$popsize-$pop_best;
$pop_rand=round(($popsize-$pop_best)/2);	//25%
$pop_mut=$popsize-($pop_best+$pop_rand);	//25%






echo "Generasi ke-";echo $gens."<br/>";
/*===========================================================================
Pembangkitan Generasi + Fitness.....
//===========================================================================*/
echo "<table border='1' style='text-align:center;'>";
echo "<tr><td></td>";
for ($a=1;$a<=$matkul;$a++)				//++
{echo "<td>".$a."</td>";}
echo "<td>fitness</td></tr>";		//++
for ($a=1;$a<=$pop_mut;$a++)
{
echo "<tr><td>Individu-".$a."</td>";				//
	for ($b=1;$b<=$matkul;$b++)
	{
		echo "<td>";									//
		$ggg[$b]=$kromosom[$a][$b]= mt_rand(1,$jadwal);
		echo $ggg[$b]."</td>";							//
	}
$fit_sum[$a]=quickCount($ggg);
$fit_av=$fit_av + $fit_sum[$a];
echo "<td>".$fit_sum[$a]."</td>";			//
echo "</tr>";								//
}

//$fit_min=max($fit_sum);
//$fit_where=inWhere($fit_sum, $fit_min);
//$fit_av2=round(($fit_av/$popsize), 5);
//echo "<tr><td>Best</td><td colspan='4'>".$fit_min."</td><td colspan='3'>Where</td><td>".$fit_where."</td></tr>";
//echo "<tr><td>Average</td><td colspan='4'>".$fit_av2."</td></tr>";
//echo "</table><br/>";
unset($fit_gbest);unset($fit_gbwhere);unset($fit_gaverage);unset($fit_av);

/*===========================================================================
mutasi.....
//===========================================================================*/

echo "<table border='1' style='text-align:center;'>";
echo "<tr><td></td>";
for ($a=1;$a<=$matkul;$a++)				//++
{echo "<td>".$a."</td>";}
echo "<td>fitness</td></tr>";		//++
for ($a=1;$a<=$pop_mut;$a++)
{
//=======================================================================
$jumlah_mut=round($pm*$matkul);$i=1;
for($aa=1;$aa<=$matkul;$aa++){
$acak=(mt_rand(0,999))/1000;
//echo $acak."-";
	if ($acak<$pm && $i<=$jumlah_mut){
	//echo "mutasi";
	$letak[$i]=$aa;$i++;
	}
}unset($i);
$ubah_mut1=$kromosom[$a][($letak[1])];$ubah_mut2=$kromosom[$a][($letak[2])];
//$ubah_mut1=$krom_pop_best[$a][($letak[1])];$ubah_mut2=$krom_pop_best[$a][($letak[2])];
//=======================================================================
echo "<tr><td>Individu-$a</td>";				//
	for ($b=1;$b<=$matkul;$b++)
	{
		echo "<td>";									//
			if($b==$letak[1]){
				$ggg[$b]=$angka2_mut[$ubah_mut1];}
			elseif($b==$letak[2]){
				$ggg[$b]=$angka2_mut[$ubah_mut2];}
			else{			
				//$ggg[$b]=$krom_pop_best[$a][$b];}
				$ggg[$b]=$kromosom[$a][$b];}
		echo $krom_pop_mut[$a][$b]=$ggg[$b]."</td>";							//
	}
//echo "<td>$letak[1] : $letak[2]</td>";
unset($letak);
$fit_sum[$a]=quickCount($ggg);
$fit_av=$fit_av + $fit_sum[$a];
if ($ubah_mut1>1){
echo "<td>".$fit_sum[$a]."*</td>";}
else{
echo "<td>".$fit_sum[$a]."</td>";}
}
echo "</tr></table>";
?>



<?php
function quickCount($makan)
{
$unik = array_unique($makan);	//dapetin angka unik
$hitung = count($unik);			//hitung ada berapa angka unik
foreach ($unik as &$value) {	//rubah angka unik; array() => array[]
    $unik2[]=$value;
	//echo $value.";";
}unset($value);
$jumlah = array_count_values($makan);	//hitung banyaknya tiap angka unik
//print_r($jumlah);
foreach ($jumlah as &$value) {	//rubah banyaknya angka unik; array() => array[]
    $jumlah2[]=$value;
	//echo $value.";";
}unset($value);
//print_r($unik);
//echo "<br/>".$hitung."<br/>";
for ($a=0;$a<$hitung;$a++)
{
if ($jumlah2[$a]==1)
{$hh[$a]=2;}
elseif ($jumlah2[$a]==2)
{$hh[$a]=1;}
else {$hh[$a]=4;}
}
$fit_slot_tot=array_sum($hh);

for ($a=0;$a<$hitung;$a++){
if ($unik2[$a]<=10){ $fit_efek[$a]=($jumlah2[$a])*1;}
elseif ($unik2[$a]<=20){ $fit_efek[$a]=($jumlah2[$a])*2;}
elseif ($unik2[$a]<=30){ $fit_efek[$a]=($jumlah2[$a])*3;}
}
$fit_efek_tot=array_sum($fit_efek);

$fit_total=round(1/($fit_slot_tot+$fit_efek_tot), 5);
return $fit_total;

/*echo "<table border='1'>";	//Cara 1
echo "<tr><td></td><td>count</td><td>fitnes-slot</td><td>fitnes-efek-slot</td></tr>";
for ($a=0;$a<$hitung;$a++){
echo "<tr><td>".$unik2[$a]."</td>";	//angka
echo "<td>".$jumlah2[$a]."</td>";	//count
echo "<td>".$hh[$a]."</td>";	//fitnes
echo "<td>".$fit_efek[$a]."</td></tr>";}
echo "<tr><td>".$fit_slot_tot."</td><td>".$fit_efek_tot."</td><td>".$fit_total."</td></tr>";
echo "</table>";*/
}


?>